<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, NexT" />








  <link rel="shortcut icon" type="image/x-icon" href="/blog/favicon.ico?v=5.1.2" />






<meta name="description" content="主要列举了工作开发中一些高频使用的 ECMAScript 6 的实用内容。参考：《ECMAScript 6 入门》 1. let 和 constlet，const 声明的变量只在它所在的代码块有效。 1.1. var 存在的问题 var 有作用域问题（会污染全局作用域） var 可以重复声明 var 会变量提升预解释 var 不能定义常量  1.2. let、const 特性 let、const">
<meta property="og:type" content="article">
<meta property="og:title" content="ECMAScript6 之常用技巧">
<meta property="og:url" content="http://xiezixi.oschina.io/2018/06/04/JS-ES6常用技巧/index.html">
<meta property="og:site_name" content="个人博客">
<meta property="og:description" content="主要列举了工作开发中一些高频使用的 ECMAScript 6 的实用内容。参考：《ECMAScript 6 入门》 1. let 和 constlet，const 声明的变量只在它所在的代码块有效。 1.1. var 存在的问题 var 有作用域问题（会污染全局作用域） var 可以重复声明 var 会变量提升预解释 var 不能定义常量  1.2. let、const 特性 let、const">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-06-04T06:43:13.769Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="ECMAScript6 之常用技巧">
<meta name="twitter:description" content="主要列举了工作开发中一些高频使用的 ECMAScript 6 的实用内容。参考：《ECMAScript 6 入门》 1. let 和 constlet，const 声明的变量只在它所在的代码块有效。 1.1. var 存在的问题 var 有作用域问题（会污染全局作用域） var 可以重复声明 var 会变量提升预解释 var 不能定义常量  1.2. let、const 特性 let、const">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://xiezixi.oschina.io/2018/06/04/JS-ES6常用技巧/"/>





  <title>ECMAScript6 之常用技巧 | 个人博客</title>
  














</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">个人博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">曦瓜皮</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>


 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://xiezixi.oschina.io/blog/2018/06/04/JS-ES6常用技巧/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="曦瓜皮">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="个人博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">ECMAScript6 之常用技巧</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-06-04T14:43:13+08:00">
                2018-06-04
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/JavaScript/" itemprop="url" rel="index">
                    <span itemprop="name">JavaScript</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>主要列举了工作开发中一些高频使用的 ECMAScript 6 的实用内容。<br>参考：<a href="http://es6.ruanyifeng.com/" target="_blank" rel="noopener">《ECMAScript 6 入门》</a></p>
<h2 id="1-let-和-const"><a href="#1-let-和-const" class="headerlink" title="1. let 和 const"></a>1. let 和 const</h2><p><code>let</code>，<code>const</code> 声明的变量只在它所在的代码块有效。</p>
<h3 id="1-1-var-存在的问题"><a href="#1-1-var-存在的问题" class="headerlink" title="1.1. var 存在的问题"></a>1.1. <code>var</code> 存在的问题</h3><ul>
<li><code>var</code> 有作用域问题（会污染全局作用域）</li>
<li><code>var</code> 可以重复声明</li>
<li><code>var</code> 会变量提升预解释</li>
<li><code>var</code> 不能定义常量</li>
</ul>
<h3 id="1-2-let、const-特性"><a href="#1-2-let、const-特性" class="headerlink" title="1.2. let、const 特性"></a>1.2. <code>let</code>、<code>const</code> 特性</h3><ul>
<li><code>let</code>、<code>const</code> 不可以重复声明</li>
<li><code>let</code>、<code>const</code> 不存在变量提升</li>
<li><code>let</code>、<code>const</code> 不会声明到全局作用域上</li>
<li><code>let</code>、<code>const</code> 存在暂时性死区（temporal dead zone，简称 TDZ）。<code>let</code> 和 <code>const</code> 声明的变量不会被提升到作用域顶部，如果在声明之前访问这些变量，会导致报错。</li>
<li><code>const</code> 做常量声明（一般常量名用大写），<code>const</code> 实际上保证的，并不是变量的值不得改动，而是变量指向的那个内存地址不得改动。</li>
</ul>
<h3 id="1-3-代码说明"><a href="#1-3-代码说明" class="headerlink" title="1.3. 代码说明"></a>1.3. 代码说明</h3><h4 id="1-3-1-作用域"><a href="#1-3-1-作用域" class="headerlink" title="1.3.1. 作用域"></a>1.3.1. 作用域</h4><p><code>var</code> 有作用域问题（会污染全局作用域），而 <code>let</code>、<code>const</code> 不会；<code>var</code> 在顶层作用域声明的变量，会直接挂在全局 <code>window</code> 对象下，成为 <code>window</code> 对象的一个属性。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// var</span></span><br><span class="line"><span class="keyword">var</span> a = <span class="number">1</span>;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">window</span>.a);</span><br><span class="line"></span><br><span class="line"><span class="comment">// let、const</span></span><br><span class="line"><span class="keyword">let</span> a = <span class="number">1</span>;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">window</span>.a); <span class="comment">// undefined</span></span><br></pre></td></tr></table></figure></p>
<h4 id="1-3-2-重复声明"><a href="#1-3-2-重复声明" class="headerlink" title="1.3.2. 重复声明"></a>1.3.2. 重复声明</h4><p><code>var</code> 可以重复声明同一个变量，但 <code>let</code>、<code>const</code> 不行；<code>let</code>、<code>const</code> 不允许在相同作用域内，重复声明同一个变量。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// var</span></span><br><span class="line"><span class="keyword">var</span> a = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">var</span> a = <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// let、const</span></span><br><span class="line"><span class="keyword">let</span> a = <span class="number">1</span>;</span><br><span class="line"><span class="keyword">let</span> a = <span class="number">2</span>; <span class="comment">// Uncaught SyntaxError: Identifier 'a' has already been declared</span></span><br></pre></td></tr></table></figure></p>
<h4 id="1-3-3-变量提升、暂时性死区"><a href="#1-3-3-变量提升、暂时性死区" class="headerlink" title="1.3.3. 变量提升、暂时性死区"></a>1.3.3. 变量提升、暂时性死区</h4><p><code>var</code> 存在变量提升、而 <code>let</code>、<code>const</code> 不存在，且 <code>let</code>、<code>const</code> 存在“暂时性死区”。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// var</span></span><br><span class="line"><span class="built_in">console</span>.log(a); <span class="comment">// undefined</span></span><br><span class="line"><span class="keyword">var</span> a = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// let、const</span></span><br><span class="line"><span class="built_in">console</span>.log(a); <span class="comment">// Uncaught ReferenceError: a is not defined</span></span><br><span class="line"><span class="keyword">let</span> a = <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 暂时性死区</span></span><br><span class="line"><span class="keyword">var</span> a = <span class="number">5</span>;</span><br><span class="line">&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(a); <span class="comment">// Uncaught ReferenceError: a is not defined</span></span><br><span class="line">    <span class="keyword">let</span> a = <span class="number">4</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h4 id="1-3-4-常量"><a href="#1-3-4-常量" class="headerlink" title="1.3.4. 常量"></a>1.3.4. 常量</h4><p><code>var</code> 不能直接定义常量，而 <code>const</code> 可以定义常量。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> a = <span class="number">5</span>;</span><br><span class="line">a = <span class="number">6</span>; <span class="comment">// Uncaught TypeError: Assignment to constant variable</span></span><br></pre></td></tr></table></figure></p>
<h2 id="2-解构赋值"><a href="#2-解构赋值" class="headerlink" title="2. 解构赋值"></a>2. 解构赋值</h2><p>ES6 允许按照一定模式，从数组和对象中提取值，对变量进行赋值，这被称为解构（Destructuring）。本质上，只要等号两边的模式相同，左边的变量就会被赋予对应的值。<br>数组，对象，字符串，数字和布尔值都能进行结构，但常用的还是对数组和对象进行结构。</p>
<h3 id="2-1-数组的解构赋值"><a href="#2-1-数组的解构赋值" class="headerlink" title="2.1. 数组的解构赋值"></a>2.1. 数组的解构赋值</h3><p>事实上，只要某种数据结构具有 Iterator 接口，都可以采用数组形式的解构赋值。如果等号的右边不是数组（或者严格地说，不是可遍历的结构），那么将会报错。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1. 通常情况</span></span><br><span class="line"><span class="keyword">let</span> [a, b, c] = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"><span class="comment">// a: 1, b: 2, c: 3</span></span><br><span class="line"><span class="keyword">let</span> [ , , c] = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"><span class="comment">// c: 3</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 2. 嵌套数组</span></span><br><span class="line"><span class="keyword">let</span> [a, [[b], c]] = [<span class="number">1</span>, [[<span class="number">2</span>], <span class="number">3</span>]];</span><br><span class="line"><span class="comment">// a: 1, b: 2, c: 3</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 3. 解构不成功，为 undefined</span></span><br><span class="line"><span class="keyword">let</span> [a] = [];</span><br><span class="line"><span class="comment">// a: undefined</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 4. 不完全解构</span></span><br><span class="line"><span class="keyword">let</span> [a, [b], d] = [<span class="number">1</span>, [<span class="number">2</span>, <span class="number">3</span>], <span class="number">4</span>];</span><br><span class="line"><span class="comment">// a: 1, b: 2, d: 4</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 5. 默认值，变量严格等于 undefined，默认值才会生效</span></span><br><span class="line"><span class="keyword">let</span> [x = <span class="number">1</span>] = [<span class="literal">undefined</span>];</span><br><span class="line">x <span class="comment">// 1</span></span><br><span class="line"><span class="keyword">let</span> [x = <span class="number">1</span>] = [<span class="literal">null</span>];</span><br><span class="line">x <span class="comment">// null</span></span><br></pre></td></tr></table></figure>
<h3 id="2-2-对象的解构赋值"><a href="#2-2-对象的解构赋值" class="headerlink" title="2.2. 对象的解构赋值"></a>2.2. 对象的解构赋值</h3><p>对象的解构与数组有一个重要的不同。数组的元素是按次序排列的，变量的取值由它的位置决定；而对象的属性没有次序，变量必须与属性同名，才能取到正确的值。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1. 通常情况</span></span><br><span class="line"><span class="keyword">let</span> &#123; foo, bar &#125; = &#123; <span class="attr">foo</span>: <span class="string">"aaa"</span>, <span class="attr">bar</span>: <span class="string">"bbb"</span> &#125;;</span><br><span class="line"><span class="comment">// foo: "aaa" , bar: "bbb"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 2. 变量名与属性名不一致</span></span><br><span class="line"><span class="keyword">let</span> &#123; <span class="attr">foo</span>: baz &#125; = &#123; <span class="attr">foo</span>: <span class="string">'aaa'</span>, <span class="attr">bar</span>: <span class="string">'bbb'</span> &#125;;</span><br><span class="line"><span class="comment">// baz: "aaa"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 3. 解构不成功，为 undefined</span></span><br><span class="line"><span class="keyword">let</span> &#123; foo &#125; = &#123;<span class="attr">bar</span>: <span class="string">'baz'</span>&#125;;</span><br><span class="line"><span class="comment">// foo: undefined</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 4. 默认值，变量严格等于 undefined，默认值才会生效</span></span><br><span class="line"><span class="keyword">var</span> &#123; <span class="attr">x</span>: y = <span class="number">3</span> &#125; = &#123;&#125;;</span><br><span class="line"><span class="comment">// y: 3</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 5. 复杂结构</span></span><br><span class="line"><span class="keyword">let</span> obj = &#123;</span><br><span class="line">  p: [</span><br><span class="line">    <span class="string">'Hello'</span>,</span><br><span class="line">    &#123; <span class="attr">y</span>: <span class="string">'World'</span> &#125;</span><br><span class="line">  ]</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> &#123; p, <span class="attr">p</span>: [x, &#123; y &#125;] &#125; = obj;</span><br><span class="line"><span class="comment">// x: "Hello", y: "World", p: ["Hello", &#123;y: "World"&#125;]</span></span><br></pre></td></tr></table></figure>
<h3 id="2-3-字符串的解构赋值"><a href="#2-3-字符串的解构赋值" class="headerlink" title="2.3. 字符串的解构赋值"></a>2.3. 字符串的解构赋值</h3><p>字符串也可以解构赋值。这是因为此时，字符串被转换成了一个类似数组的对象。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> [a, b, c, d, e, length] = <span class="string">'hello'</span>;</span><br><span class="line"><span class="comment">// a: "h" .... length: 5</span></span><br></pre></td></tr></table></figure></p>
<h2 id="3-箭头函数"><a href="#3-箭头函数" class="headerlink" title="3. 箭头函数"></a>3. 箭头函数</h2><h3 id="3-1-什么是箭头函数"><a href="#3-1-什么是箭头函数" class="headerlink" title="3.1. 什么是箭头函数"></a>3.1. 什么是箭头函数</h3><h4 id="3-1-1-箭头函数简介"><a href="#3-1-1-箭头函数简介" class="headerlink" title="3.1.1. 箭头函数简介"></a>3.1.1. 箭头函数简介</h4><p>箭头函数的语法非常简单 <code>(parameters) =&gt; { statements }</code>，箭头函数只能用赋值式写法，不能用声明式写法。看一下最简单的箭头函数表示法：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> sayHello = <span class="function"><span class="params">()</span> =&gt;</span> &#123; <span class="built_in">console</span>.log(<span class="string">'Hello World'</span>); &#125;</span><br><span class="line">sayHello(); <span class="comment">// Hello World</span></span><br></pre></td></tr></table></figure></p>
<h4 id="3-1-2-箭头函数的优点"><a href="#3-1-2-箭头函数的优点" class="headerlink" title="3.1.2. 箭头函数的优点"></a>3.1.2. 箭头函数的优点</h4><p>和普通函数相比，箭头函数主要就是以下两个方面的特点：</p>
<ul>
<li>更简化的代码语法</li>
<li>没有局部 this 的绑定</li>
<li>不绑定 arguments</li>
</ul>
<h3 id="3-2-更简化的代码语法"><a href="#3-2-更简化的代码语法" class="headerlink" title="3.2. 更简化的代码语法"></a>3.2. 更简化的代码语法</h3><h4 id="3-2-1-一般写法"><a href="#3-2-1-一般写法" class="headerlink" title="3.2.1. 一般写法"></a>3.2.1. 一般写法</h4><p><code>(parameters) =&gt; { statements }</code><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 求两个数的和并在控制台输出</span></span><br><span class="line"><span class="comment">// ES6</span></span><br><span class="line"><span class="keyword">let</span> sum = <span class="function">(<span class="params">a, b</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(a + b);</span><br><span class="line">  <span class="keyword">return</span> a + b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// ES5</span></span><br><span class="line"><span class="keyword">var</span> sum = <span class="function"><span class="keyword">function</span>(<span class="params">a, b</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(a + b);</span><br><span class="line">  <span class="keyword">return</span> a + b;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h4 id="3-2-2-省略-的情况"><a href="#3-2-2-省略-的情况" class="headerlink" title="3.2.2. 省略 () 的情况"></a>3.2.2. 省略 () 的情况</h4><p>如果只有一个参数，可以省略<code>()</code>括号。<br><code>parameters =&gt; { statements }</code><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 求一个数的两倍，并输出</span></span><br><span class="line"><span class="comment">// ES6</span></span><br><span class="line"><span class="keyword">let</span> double = <span class="function"><span class="params">num</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(num * <span class="number">2</span>);</span><br><span class="line">  <span class="keyword">return</span> num * <span class="number">2</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// ES5</span></span><br><span class="line"><span class="keyword">var</span> double = <span class="function"><span class="keyword">function</span>(<span class="params">num</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(num * <span class="number">2</span>);</span><br><span class="line">  <span class="keyword">return</span> num * <span class="number">2</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h4 id="3-2-3-省略-的情况"><a href="#3-2-3-省略-的情况" class="headerlink" title="3.2.3. 省略 {} 的情况"></a>3.2.3. 省略 {} 的情况</h4><p>如果返回值仅仅只有一个表达式（expression）, 还可以省略大括号<code>{}</code>。<br><code>parameters =&gt; statements</code><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 求一个数的两倍</span></span><br><span class="line"><span class="comment">// ES6</span></span><br><span class="line"><span class="keyword">let</span> double = <span class="function"><span class="params">num</span> =&gt;</span> num * <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// ES5</span></span><br><span class="line"><span class="keyword">var</span> double = <span class="function"><span class="keyword">function</span>(<span class="params">num</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> num * <span class="number">2</span>;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h4 id="3-2-4-特殊情况"><a href="#3-2-4-特殊情况" class="headerlink" title="3.2.4. 特殊情况"></a>3.2.4. 特殊情况</h4><p>如果箭头函数直接返回一个对象，必须在对象外面加上括号。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ES6</span></span><br><span class="line"><span class="keyword">let</span> returnObj = <span class="function"><span class="params">()</span> =&gt;</span> (&#123;<span class="attr">name</span>: <span class="string">"xiguapi"</span>, <span class="attr">age</span>: <span class="number">23</span>&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// ES5</span></span><br><span class="line"><span class="keyword">var</span> returnObj = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">        name: <span class="string">"xiguapi"</span>,</span><br><span class="line">        age: <span class="number">23</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<h3 id="3-3-没有局部-this-的绑定"><a href="#3-3-没有局部-this-的绑定" class="headerlink" title="3.3. 没有局部 this 的绑定"></a>3.3. 没有局部 this 的绑定</h3><p>和一般的函数不同，箭头函数不会绑定 <code>this</code>。 或则说箭头函数不会改变 <code>this</code> 本来的绑定。<br>我们用一个例子来说明：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Counter</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">this</span>.num = <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> a = <span class="keyword">new</span> Counter(); <span class="comment">// a.num 0</span></span><br></pre></td></tr></table></figure></p>
<p>因为使用了关键字 <code>new</code> 构造，<code>Count()</code> 函数中的 <code>this</code>绑定到一个新的对象，并且赋值给 <code>a</code>。通过 <code>console.log</code> 打印 <code>a.num</code>，会输出 0。</p>
<p>如果我们想每过一秒将 <code>a.num</code> 的值加 1，该如何实现呢？可以使用 <code>setInterval()</code> 函数。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Counter</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">this</span>.num = <span class="number">0</span>;</span><br><span class="line">  <span class="keyword">this</span>.timer = setInterval(<span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.num++;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="keyword">this</span>.num);</span><br><span class="line">  &#125;, <span class="number">1000</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> b = <span class="keyword">new</span> Counter();</span><br><span class="line"><span class="comment">// NaN</span></span><br><span class="line"><span class="comment">// NaN</span></span><br><span class="line"><span class="comment">// NaN</span></span><br><span class="line"><span class="comment">// ...</span></span><br></pre></td></tr></table></figure></p>
<p>首先函数 <code>setInterval</code> 没有被某个声明的对象调用，也没有使用 <code>new</code> 关键字，再之没有使用 <code>bind</code>, <code>call</code> 和 <code>apply</code>。<code>setInterval</code> 只是一个普通的函数。实际上 <code>setInterval</code> 里面的 <code>this</code> 绑定到全局对象 <code>window</code> 上了。</p>
<p>使用箭头函数！使用箭头函数就不会导致 <code>this</code> 被绑定到全局对象。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Counter</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">this</span>.num = <span class="number">0</span>;</span><br><span class="line">  <span class="keyword">this</span>.timer = setInterval(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">this</span>.num++;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="keyword">this</span>.num);</span><br><span class="line">  &#125;, <span class="number">1000</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> b = <span class="keyword">new</span> Counter();</span><br><span class="line"><span class="comment">// 1</span></span><br><span class="line"><span class="comment">// 2</span></span><br><span class="line"><span class="comment">// 3</span></span><br><span class="line"><span class="comment">// ...</span></span><br></pre></td></tr></table></figure></p>
<p>通过 <code>Counter</code> 构造函数绑定的 <code>this</code> 将会被保留。在 <code>setInterval</code> 函数中，<code>this</code> 依然指向我们新创建的 <code>b</code> 对象。</p>
<h3 id="3-3-不绑定-arguments"><a href="#3-3-不绑定-arguments" class="headerlink" title="3.3. 不绑定 arguments"></a>3.3. 不绑定 arguments</h3><p>箭头函数还有一个比较有特点的地方就是其不绑定 arguments,即如果你在箭头函数中使用 arguments 参数不能得到想要的内容。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arrowfunc = <span class="function"><span class="params">()</span> =&gt;</span> <span class="built_in">console</span>.log(<span class="built_in">arguments</span>.length)</span><br><span class="line">arrowfunc()</span><br><span class="line"><span class="comment">// arguments is not defined</span></span><br></pre></td></tr></table></figure></p>
<h3 id="3-4-适用场景及注意点"><a href="#3-4-适用场景及注意点" class="headerlink" title="3.4. 适用场景及注意点"></a>3.4. 适用场景及注意点</h3><ul>
<li>箭头函数适合于无复杂逻辑或者无副作用的纯函数场景下，例如：用在 map、reduce、filter 的回调函数定义中</li>
<li>箭头函数的亮点是简洁，但在有多层函数嵌套的情况下，箭头函数反而影响了函数的作用范围的识别度，这种情况不建议使用箭头函数</li>
<li>箭头函数要实现类似纯函数的效果，必须剔除外部状态。所以箭头函数不具备普通函数里常见的 this、arguments 等，当然也就不能用 call()、apply()、bind() 去改变 this 的指向</li>
<li>箭头函数不适合定义对象的方法（对象字面量方法、对象原型方法、构造器方法），因为箭头函数没有自己的 this，其内部的 this 指向的是外层作用域的 this</li>
<li>箭头函数不适合定义结合动态上下文的回调函数（事件绑定函数），因为箭头函数在声明的时候会绑定静态上下文</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> json = &#123;<span class="attr">bar</span>: <span class="number">1</span>, <span class="attr">fn</span>: <span class="function"><span class="params">()</span> =&gt;</span> <span class="built_in">console</span>.log(<span class="keyword">this</span>.bar)&#125;;</span><br><span class="line">json.fn(); <span class="comment">//-&gt; undefined</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Foo</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.bar = <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Foo.prototype.fn = <span class="function"><span class="params">()</span> =&gt;</span> <span class="built_in">console</span>.log(<span class="keyword">this</span>.foo);</span><br><span class="line"><span class="keyword">const</span> foo = <span class="keyword">new</span> Foo();</span><br><span class="line">foo.fn(); <span class="comment">//-&gt; undefined // this 并不是指向 Foo，根据变量查找规则，回溯到了全局作用域</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> Message = <span class="function">(<span class="params">text</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">this</span>.text = text;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">var</span> helloMessage = <span class="keyword">new</span> Message(<span class="string">'Hello World!'</span>);</span><br><span class="line"><span class="built_in">console</span>.log(helloMessage.text); <span class="comment">//-&gt; Message is not a constructor // 不可以当作构造函数，也就是说，不可以使用 new 命令</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> button = <span class="built_in">document</span>.querySelector(<span class="string">'button'</span>);</span><br><span class="line">button.addEventListener(<span class="string">'click'</span>, () =&gt; &#123;</span><br><span class="line">    <span class="keyword">this</span>.textContent = <span class="string">'Loading...'</span>;</span><br><span class="line">&#125;); <span class="comment">// this 并不是指向预期的 button 元素，而是 window</span></span><br></pre></td></tr></table></figure>
<h2 id="4-字符串"><a href="#4-字符串" class="headerlink" title="4. 字符串"></a>4. 字符串</h2><h3 id="4-1-includes-方法"><a href="#4-1-includes-方法" class="headerlink" title="4.1. includes 方法"></a>4.1. includes 方法</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 判断字符串中是否包含某个字符串</span></span><br><span class="line"><span class="keyword">let</span> str = <span class="string">'javascript'</span>;</span><br><span class="line">str.includes(<span class="string">'sc'</span>); <span class="comment">// true</span></span><br></pre></td></tr></table></figure>
<h3 id="4-2-endsWith、startsWith-方法"><a href="#4-2-endsWith、startsWith-方法" class="headerlink" title="4.2. endsWith、startsWith 方法"></a>4.2. endsWith、startsWith 方法</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 判断字符串是否以某一个字符串开始或结束</span></span><br><span class="line"><span class="keyword">var</span> a = <span class="string">'1AB2345CD'</span>;</span><br><span class="line"><span class="built_in">console</span>.log(a.startsWith(<span class="string">'1A'</span>)); <span class="comment">// true</span></span><br><span class="line">a.endsWith(<span class="string">'cD'</span>) <span class="comment">// false</span></span><br></pre></td></tr></table></figure>
<h3 id="4-3-字符串内容重复输出"><a href="#4-3-字符串内容重复输出" class="headerlink" title="4.3. 字符串内容重复输出"></a>4.3. 字符串内容重复输出</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'a'</span>.repeat(<span class="number">5</span>);<span class="comment">//aaaaa 重复输出5遍</span></span><br></pre></td></tr></table></figure>
<h3 id="4-4-字符串补全"><a href="#4-4-字符串补全" class="headerlink" title="4.4. 字符串补全"></a>4.4. 字符串补全</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 参数1：[number] 目标字符串长度</span></span><br><span class="line"><span class="comment">// 参数2：[string] 进行补全的字符串</span></span><br><span class="line"><span class="string">'12345'</span>.padStart(<span class="number">7</span>, <span class="string">'0'</span>) <span class="comment">// 0012345 - 字符串不足7位，在头部补充不足长度的目标字符串</span></span><br><span class="line"><span class="string">'12345'</span>.padEnd(<span class="number">7</span>, <span class="string">'0'</span>) <span class="comment">// 1234500 - 在尾部进行字符串补全</span></span><br></pre></td></tr></table></figure>
<h3 id="4-5-原生支持模板语言"><a href="#4-5-原生支持模板语言" class="headerlink" title="4.5. 原生支持模板语言"></a>4.5. 原生支持模板语言</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// es5</span></span><br><span class="line">$(<span class="string">'#result'</span>).append(</span><br><span class="line">  <span class="string">'There are &lt;b&gt;'</span> + basket.count + <span class="string">'&lt;/b&gt; '</span> +</span><br><span class="line">  <span class="string">'items in your basket, '</span> +</span><br><span class="line">  <span class="string">'&lt;em&gt;'</span> + basket.onSale +</span><br><span class="line">  <span class="string">'&lt;/em&gt; are on sale!'</span></span><br><span class="line">);</span><br><span class="line"><span class="comment">// es6</span></span><br><span class="line"><span class="comment">// 在es6中，内容模板，可以定义在 `` 包起来的字符串中，其中的内容会保持原有格式</span></span><br><span class="line"><span class="comment">// 另外可以在字符串中直接使用模板语言进行变量填充，优雅而简洁</span></span><br><span class="line">$(<span class="string">'#result'</span>).append(<span class="string">`</span></span><br><span class="line"><span class="string">  There are &lt;b&gt;<span class="subst">$&#123;basket.count&#125;</span>&lt;/b&gt; items</span></span><br><span class="line"><span class="string">   in your basket, &lt;em&gt;<span class="subst">$&#123;basket.onSale&#125;</span>&lt;/em&gt;</span></span><br><span class="line"><span class="string">  are on sale!</span></span><br><span class="line"><span class="string">`</span>);</span><br></pre></td></tr></table></figure>
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/06/01/自动化-Jenkins/" rel="next" title="自动化构建之 Jenkins™">
                <i class="fa fa-chevron-left"></i> 自动化构建之 Jenkins™
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/06/04/ES6-箭头函数/" rel="prev" title="ECMAScript6 之箭头函数™">
                ECMAScript6 之箭头函数™ <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/blog/images/avatar.jpg"
               alt="曦瓜皮" />
          <p class="site-author-name" itemprop="name">曦瓜皮</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/blog/archives/">
                <span class="site-state-item-count">15</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/blog/categories/index.html">
                <span class="site-state-item-count">6</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        

      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-let-和-const"><span class="nav-number">1.</span> <span class="nav-text">1. let 和 const</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-var-存在的问题"><span class="nav-number">1.1.</span> <span class="nav-text">1.1. var 存在的问题</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-let、const-特性"><span class="nav-number">1.2.</span> <span class="nav-text">1.2. let、const 特性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-代码说明"><span class="nav-number">1.3.</span> <span class="nav-text">1.3. 代码说明</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-1-作用域"><span class="nav-number">1.3.1.</span> <span class="nav-text">1.3.1. 作用域</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-2-重复声明"><span class="nav-number">1.3.2.</span> <span class="nav-text">1.3.2. 重复声明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-3-变量提升、暂时性死区"><span class="nav-number">1.3.3.</span> <span class="nav-text">1.3.3. 变量提升、暂时性死区</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-4-常量"><span class="nav-number">1.3.4.</span> <span class="nav-text">1.3.4. 常量</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-解构赋值"><span class="nav-number">2.</span> <span class="nav-text">2. 解构赋值</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-数组的解构赋值"><span class="nav-number">2.1.</span> <span class="nav-text">2.1. 数组的解构赋值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-对象的解构赋值"><span class="nav-number">2.2.</span> <span class="nav-text">2.2. 对象的解构赋值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-字符串的解构赋值"><span class="nav-number">2.3.</span> <span class="nav-text">2.3. 字符串的解构赋值</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-箭头函数"><span class="nav-number">3.</span> <span class="nav-text">3. 箭头函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-什么是箭头函数"><span class="nav-number">3.1.</span> <span class="nav-text">3.1. 什么是箭头函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-1-箭头函数简介"><span class="nav-number">3.1.1.</span> <span class="nav-text">3.1.1. 箭头函数简介</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-2-箭头函数的优点"><span class="nav-number">3.1.2.</span> <span class="nav-text">3.1.2. 箭头函数的优点</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-更简化的代码语法"><span class="nav-number">3.2.</span> <span class="nav-text">3.2. 更简化的代码语法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-1-一般写法"><span class="nav-number">3.2.1.</span> <span class="nav-text">3.2.1. 一般写法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-2-省略-的情况"><span class="nav-number">3.2.2.</span> <span class="nav-text">3.2.2. 省略 () 的情况</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-3-省略-的情况"><span class="nav-number">3.2.3.</span> <span class="nav-text">3.2.3. 省略 {} 的情况</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-4-特殊情况"><span class="nav-number">3.2.4.</span> <span class="nav-text">3.2.4. 特殊情况</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-没有局部-this-的绑定"><span class="nav-number">3.3.</span> <span class="nav-text">3.3. 没有局部 this 的绑定</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-不绑定-arguments"><span class="nav-number">3.4.</span> <span class="nav-text">3.3. 不绑定 arguments</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4-适用场景及注意点"><span class="nav-number">3.5.</span> <span class="nav-text">3.4. 适用场景及注意点</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-字符串"><span class="nav-number">4.</span> <span class="nav-text">4. 字符串</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-includes-方法"><span class="nav-number">4.1.</span> <span class="nav-text">4.1. includes 方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-endsWith、startsWith-方法"><span class="nav-number">4.2.</span> <span class="nav-text">4.2. endsWith、startsWith 方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-3-字符串内容重复输出"><span class="nav-number">4.3.</span> <span class="nav-text">4.3. 字符串内容重复输出</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-4-字符串补全"><span class="nav-number">4.4.</span> <span class="nav-text">4.4. 字符串补全</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-5-原生支持模板语言"><span class="nav-number">4.5.</span> <span class="nav-text">4.5. 原生支持模板语言</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">曦瓜皮</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.2"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.2"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.2"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.2"></script>



  


  




	





  





  






  





  

  

  

  

  

  

</body>
</html>
